/***
This do-file creates a figure that compares Advance Monthly Retail Trade Survey 
(MARTS) data on consumer spending on retail sales and food services (excluding 
motor vehicle and gas) with vs without seasonal adjustment. 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Spending"
local category "Spending"

* Create required subfolders
cap mkdir "${root}/results/Spending"
cap mkdir "${root}/results/paper numbers"
cap mkdir "${root}/results/paper numbers/`category'"

* Erase output numbers
cap erase "${root}/results/paper numbers/`category'/Seasonal Fluctuations in Consumer Spending in MARTS Data.yaml"

*-------------------------------------------------------------------------------
* Import MARTS not seasonally adjusted
*-------------------------------------------------------------------------------
 
project, uses("${root}/data/derived/MARTS/MARTS monthly sales by industry.dta")
use "${root}/data/derived/MARTS/MARTS monthly sales by industry.dta", clear

gen year = year(dofm(month))
gen month_d = month(dofm(month))
gen day = day(dofm(month))
drop month
rename month_d month
gen date = mdy(month, day, year)

keep if is_adj == 0 & year>2009 & cat_code == "44X72"

gen temp = 1 if month == 1
bysort year (date): gegen base = first(sales)
gen retail_non_adjust_norm = (sales / base -1) *100
drop temp base

tempfile marts_nonadjusted
save `marts_nonadjusted'

*-------------------------------------------------------------------------------
* Import MARTS seasonally adjusted 
*-------------------------------------------------------------------------------

project, uses("${root}/data/derived/MARTS/MARTS monthly sales by industry.dta")
use "${root}/data/derived/MARTS/MARTS monthly sales by industry.dta", clear

gen year = year(dofm(month))
gen month_d = month(dofm(month))
gen day = day(dofm(month))
drop month
rename month_d month
gen date = mdy(month, day, year)

keep if is_adj == 1 & year>2009 & cat_code == "44X72"

destring year month day, force replace

gen temp = 1 if month == 1
bysort year (date): gegen base = first(sales)
gen retail_adjust_norm = (sales / base -1) * 100
drop temp base

merge 1:1 date month day year using `marts_nonadjusted', nogen

keep if year < 2020

* Calculate RMSE
gen diff = retail_adjust_norm - retail_non_adjust_norm
gen diff2 = diff * diff
sum diff2 if month != 1

local mse = `r(mean)'
local rmse: di %3.2f round(sqrt(`mse'), 0.01)

di `rmse'

sort date

forval year = 2010/2020{
 	local thisdate = mdy(1,1,`year')
 	local mydates `mydates' `thisdate'
}

local rmse_text_pos = mdy(6,1,2018)

*-------------------------------------------------------------------------------
* Plot 
*-------------------------------------------------------------------------------

tw ///
	(connected retail_non_adjust_norm date, sort color(oi1) msymbol (T)) ///
	(connected retail_adjust_norm date, sort color(oi2)) ///
	, /// 
	xlab(`mydates', format(%tdCCYY) labsize(medsmall)) xtitle(" ") ${title_`version'} ///
	legend(order(1 "No Seasonal Adjustment" 2 "With Seasonal Adjustment") size(medsmall) col(1) ring(0) pos(8) symxsize(huge)) ///
	ylabel(-20 "-20%" 0 "0%" 20 "+20%" 40 "+40%", nogrid) ///
	ytitle("Change in Spending (%)" "Relative to January") ///
	text(-18 `rmse_text_pos' "RMSE: `rmse' p.p." ///
	, size(medsmall) color(gs6) justification(left)) 
oi_graph_export "${root}/results/Spending/Seasonal Fluctuations in Consumer Spending in MARTS", type(${fig_type})

*-------------------------------------------------------------------------------
* Export output numbers to csv file
*-------------------------------------------------------------------------------

yamlout using "${root}/results/paper numbers/`category'/Seasonal Fluctuations in Consumer Spending in MARTS Data.yaml", ///
	key("seas_spend_rmse") ///
	comment("RMSE") ///
	value(`rmse') fmt(%9.2f)

project, creates("${root}/results/paper numbers/`category'/Seasonal Fluctuations in Consumer Spending in MARTS Data.yaml")